from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
import unittest,time
from unint_6.mysql import Mysql

# 定义类
class JiuXian(unittest.TestCase):
    # TestFixture测试固件
    def setUp(self):
        # 准备环境
        self.driver=webdriver.Edge()
        self.driver.get('http://10.59.9.14:7003/')
        self.driver.maximize_window()
        self.db=Mysql(host='10.59.9.14',user='root',password='yidong666',database='a3mall')


    def tearDown(self):
        # 清理环境
        time.sleep(10)
        self.driver.quit()
        self.db.close()
    # TestCase 测试用例
    def test_01_login(self):
        self.driver.find_element(By.LINK_TEXT, '登录').click()
        time.sleep(2)
        # 输入账号密码
        self.driver.find_element(By.NAME, 'username').send_keys('189lianxi')
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        time.sleep(10)
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, '.finish-btn').click()
        time.sleep(5)

        # 数据库断言
        # 断言1欢迎回来
        dy1=self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print('断言1', dy1)
        alldata= self.db.chaxun(f'select username from mall_users where mobile="189lianxi"')
        print(alldata[0][0])
        assert alldata[0][0] in dy1
        # 断言2金额
        dy2=self.driver.find_element(By.CLASS_NAME,'color-black').text
        dy2a=dy2.split()[1]
        print('断言2',dy2a)
        alldata1=self.db.chaxun('select amount from mall_users where mobile="189lianxi"')
        print(alldata1[0][0])
        self.assertEqual(float(dy2a),float(alldata1[0][0]))
        time.sleep(2)
        # # 断言积分
        # dy3=self.driver.find_element(By.CLASS_NAME,'color-orage').text
        # dy3a=dy3.split()[0]
        # print('断言3',dy3a)
        # alldata2=self.db.chaxun('select point from mall_users where moblie="189lianxi"')
        # print(alldata2)
        # # self.assertEqual(dy3a,alldata2[0][0])



        # 点击首页
        self.driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/ul/li[1]/a').click()
        time.sleep(3)
        # 搜索商品——-山西汾酒
        search = self.driver.find_element(By.CLASS_NAME, 'search-input')
        search.send_keys("山西汾酒")
        time.sleep(3)
        search.send_keys(Keys.ENTER)
        time.sleep(4)
        # 点击商品进入商品详情页面
        self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img').click()
        # 点击购买
        self.driver.find_element(By.ID, 'buy-cart').click()
        time.sleep(2)
        # 滑动800
        self.driver.execute_script('window.scrollTo(0,800)')
        time.sleep(2)
        # 点击下一步
        self.driver.find_element(By.CLASS_NAME, 'ask-btn').click()
        time.sleep(3)
        # 点击查看订单
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
        time.sleep(2)
        # dy=driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[2]/table/tbody/tr[3]/td[1]').text
        # print("A3Mall订单编号:",dy)
        # 订单编号
        dy = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
        print("A3Mall订单编号:", dy)
        # 点击会员中心
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[3]/a').click()
        time.sleep(5)
        self.driver.refresh()
        time.sleep(10)
        # 从数据库中拿取数据
        data = self.db.chaxun(f'select order_no from mall_order where user_id="2277"')
        print("数据库订单编号:", data)
        # self.assertIn(dy,data[0][6])